package com.rustic.server.controller.admapi;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.CharSequenceUtil;
import cn.xy.base.server.utils.PageUtil;
import com.rustic.server.query.UserBrowseLinkAdminListQuery;
import com.rustic.server.dto.UserBrowseLinkAddDTO;
import com.rustic.server.dto.UserBrowseLinkUpdateDTO;
import com.rustic.server.po.UserBrowseLink;
import com.rustic.server.service.UserBrowseLinkService;
import com.rustic.server.vo.list.UserBrowseLinkAdminListVO;
import com.rustic.server.vo.UserBrowseLinkVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import cn.xy.commons.annotation.OperateLog;
import cn.xy.commons.core.Result;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 表[user_browse_link]对应的后台管理类
 *
 * @author auto
 *
 */

@Slf4j
@RestController
@RequestMapping("/admapi/userBrowseLink")
@Api(tags = {"用户浏览关联管理接口"})
public class UserBrowseLinkAdminController {

    @Autowired
    private UserBrowseLinkService userBrowseLinkService;

    @ApiOperation(value = "用户浏览关联-列表", notes = "权限key: user-browse-link-list")
    @GetMapping("/list")
    @PreAuthorize("@validate.hasPermission('user-browse-link-list')")
    public Result<Page<UserBrowseLinkAdminListVO>> list(@Validated UserBrowseLinkAdminListQuery query) {
        LambdaQueryWrapper<UserBrowseLink> queryWrapper = userBrowseLinkService.toAdminListWrapper(query);
        if (queryWrapper == null) {
            return Result.data(new Page<>());
        }
        Page<UserBrowseLink> pageData = userBrowseLinkService.page(query.toPage(), queryWrapper);
        List<UserBrowseLink> userBrowseLinkList = pageData.getRecords();
        if (CollUtil.isEmpty(userBrowseLinkList)) {
            return Result.data(new Page<>());
        }
        List<UserBrowseLinkAdminListVO> results = userBrowseLinkService.toAdminListVo(userBrowseLinkList);
        return Result.data(PageUtil.toPage(pageData, results));
    }

    @ApiOperation(value = "用户浏览关联-详情", notes = "权限key: user-browse-link-detail")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "用户浏览关联id"),
    })
    @GetMapping("/detail")
    @PreAuthorize("@validate.hasPermission('user-browse-link-detail')")
    public Result<UserBrowseLinkVO> detail(int id) {
        if (id <= 0) {
            return Result.error(UserBrowseLinkService.USER_BROWSE_LINK_NOT_EXIST);
        }
        UserBrowseLink userBrowseLink = userBrowseLinkService.getById(id);
        if (userBrowseLink == null) {
            return Result.error(UserBrowseLinkService.USER_BROWSE_LINK_NOT_EXIST);
        }
        UserBrowseLinkVO userBrowseLinkVO = new UserBrowseLinkVO(userBrowseLink);
        return Result.data(userBrowseLinkVO);
    }

    @ApiOperation(value = "用户浏览关联-增加", notes = "权限key: user-browse-link-add")
    @PostMapping("/add")
    @PreAuthorize("@validate.hasPermission('user-browse-link-add')")
    @OperateLog(description = "增加用户浏览关联")
    public Result<String> add(@RequestBody @Validated UserBrowseLinkAddDTO userBrowseLinkAddDTO) {
        UserBrowseLink userBrowseLink = userBrowseLinkAddDTO.toUserBrowseLink();
        userBrowseLinkService.save(userBrowseLink);
        return Result.ok("添加用户浏览关联成功");
    }

    @ApiOperation(value = "用户浏览关联-更新", notes = "权限key: user-browse-link-update")
    @PutMapping("/update")
    @PreAuthorize("@validate.hasPermission('user-browse-link-update')")
    @OperateLog(description = "更新用户浏览关联")
    public Result<String> update(@RequestBody @Validated UserBrowseLinkUpdateDTO userBrowseLinkUpdateDTO) {
        UserBrowseLink userBrowseLink = userBrowseLinkService.getById(userBrowseLinkUpdateDTO.getId());
        if (userBrowseLink == null) {
            return Result.error(UserBrowseLinkService.USER_BROWSE_LINK_NOT_EXIST);
        }
        BeanUtils.copyProperties(userBrowseLinkUpdateDTO, userBrowseLink);
        userBrowseLinkService.updateById(userBrowseLink);
        return Result.ok("编辑用户浏览关联成功");
    }

    @ApiOperation(value = "用户浏览关联-删除", notes = "权限key: user-browse-link-delete")
    @DeleteMapping("/delete")
    @PreAuthorize("@validate.hasPermission('user-browse-link-delete')")
    @OperateLog(description = "删除用户浏览关联")
    public Result<String> delete(int id) {
        if (id <= 0) {
            return Result.error(UserBrowseLinkService.USER_BROWSE_LINK_NOT_EXIST);
        }
        UserBrowseLink userBrowseLink = userBrowseLinkService.getById(id);
        if (userBrowseLink == null) {
            return Result.error(UserBrowseLinkService.USER_BROWSE_LINK_NOT_EXIST);
        }
        userBrowseLinkService.removeById(id);
        return Result.ok("删除用户浏览关联成功");
    }
}
